home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / filenation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-17  |  6.1 KB  |  242 lines

  1. /*
  2.     FileNation XFCN v1.0
  3.     
  4.     ©1991 Apple Computer, Inc.; by Mike Byrne
  5.     
  6.     Given a full pathname, this XFCN will go out and search for a 'vers' resource, and then
  7.     pull the country code out of it if there is one.  No problem...
  8.     
  9.     Form:
  10.     FileNation(<pathName>)
  11.     
  12.     # the MPW 3.2 build commands:
  13.     C -b FileNation.c -mbg off
  14.         Link -w -t STAK -c WILD -rt XFCN=614 ∂
  15.             -m ENTRYPOINT ∂
  16.             -sg FileNation ∂
  17.             FileNation.c.o ∂
  18.             "{Libraries}HyperXLib.o" ∂
  19.             "{Libraries}Runtime.o" ∂
  20.             "{Libraries}Interface.o" ∂
  21.             "{CLibraries}StdCLib.o" ∂
  22.             -o "teststack"
  23. */
  24.  
  25. #include <Types.h>
  26. #include <Resources.h>
  27. #include <string.h>
  28. #include <Memory.h>
  29. #include <Packages.h>
  30. #include "HyperXCmd.h"
  31.  
  32. #define NULL (long) 0
  33. #define NIL (long) 0
  34.  
  35. #define kNumParams 1
  36. #define    FALSE 0
  37. #define TRUE 1
  38.  
  39.  
  40. /* prototypes */
  41. void ErrorBack(XCmdPtr paramPtr, char *message);
  42. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  43. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  44.  
  45.  
  46.  
  47. pascal void EntryPoint(XCmdPtr paramPtr)
  48. {
  49.     /* variable declarations */
  50.     short        nationCode;
  51.     Str255        nationStr;
  52.     short         i,j;
  53.     char        volName[34];
  54.     char        ppathName[260];
  55.     short        vRefNum;
  56.     Boolean*    orgResLoadPtr;
  57.     Boolean        orgResLoad;
  58.     short        orgResFile;
  59.     short        theResFile;
  60.     short        theError;
  61.     VersRecHndl    versHandle;
  62.  
  63.  
  64.     /* move high and lock the parameters. */
  65.     MoveLockParams(paramPtr, paramPtr->paramCount);
  66.  
  67.     /* check for copyright or syntax help request */
  68.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  69.         ErrorBack(paramPtr, "v1.0, ©1991 Apple Computer, Inc.; by Mike Byrne");
  70.         UnlockParams(paramPtr, paramPtr->paramCount);
  71.         return;
  72.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  73.         ErrorBack(paramPtr, "FileNation syntax is 'FileNation(<pathname>'");
  74.         UnlockParams(paramPtr, paramPtr->paramCount);
  75.         return;
  76.     }
  77.  
  78.     /* not a copyright or help request.       */     
  79.     /* check for correct number of parameters */
  80.     if (paramPtr->paramCount != kNumParams) {
  81.         ErrorBack(paramPtr, "Error: FileNation syntax is 'FileNation(<pathname>)'");
  82.         UnlockParams(paramPtr, paramPtr->paramCount);
  83.         return;
  84.     }
  85.  
  86.     /*  extract the volume name from the handle, copy to a pas string,
  87.          and get the volume reference number of the volume              */
  88.     for (i=0; ((*(paramPtr->params[0]))[i] != ':' && (i < 33)); i++) 
  89.         { volName[i] = (*(paramPtr->params[0]))[i]; }
  90.     volName[i] = ':';
  91.     volName[i+1] = '\0'; 
  92.     c2pstr(volName);
  93.     vRefNum = 0;
  94.     
  95.     if (SetVol(volName, vRefNum) != noErr) {                        // toolbox
  96.         ErrorBack(paramPtr, "Error: Could not set the default volume");
  97.         UnlockParams(paramPtr, kNumParams);
  98.         return;
  99.     }
  100.     
  101.     if (GetVol(&volName, &vRefNum) != noErr) {                        // toolbox
  102.         ErrorBack(paramPtr, "Error: Could not find the volume requested.");
  103.         UnlockParams(paramPtr, kNumParams);
  104.         return;
  105.     }
  106.  
  107.     /* now, copy the rest of the pathname to the partial pathname and convert it. */
  108.     for (j=i; (j <= strlen((*(paramPtr->params[0]))) && (j < 300)); j++) 
  109.         { ppathName[j-i] = (*(paramPtr->params[0]))[j]; }
  110.     c2pstr(ppathName);
  111.         
  112.         
  113.     /* First, save the old state. */
  114.     orgResFile = CurResFile();
  115.     (long) orgResLoadPtr = 0xA5E;
  116.     if (*orgResLoadPtr) {
  117.         orgResLoad = TRUE;
  118.     } else {
  119.         orgResLoad = FALSE;
  120.     }
  121.  
  122.     /* flip the ResLoad (for efficiency), and open the file. */
  123.     SetResLoad(FALSE);
  124.     theResFile = OpenRFPerm(ppathName, vRefNum, fsRdPerm);
  125.     theError = ResError();
  126.     
  127.     /* check if there is even a resource fork. */
  128.     if (theError == -39) {
  129.         SetResLoad(orgResLoad);
  130.         CloseResFile(theResFile);
  131.         ErrorBack(paramPtr, "Error: The file has no resource fork.");
  132.         UnlockParams(paramPtr, kNumParams);
  133.         return;
  134.     }
  135.     
  136.     /* check for anything else */
  137.     if (theError != noErr) {
  138.         SetResLoad(orgResLoad);
  139.         CloseResFile(theResFile);
  140.         ErrorBack(paramPtr, "Error: The file could not be found or opened.");
  141.         UnlockParams(paramPtr, kNumParams);
  142.         return;
  143.     }
  144.     
  145.     
  146.     /* okay, we're cool for now.  Set up the resload and resfile. */
  147.     SetResLoad(TRUE);
  148.     UseResFile(theResFile);
  149.     
  150.     /* check for 'vers' resources */
  151.     if (Count1Resources('vers') == 0) {
  152.         UseResFile(orgResFile);
  153.         SetResLoad(orgResLoad);
  154.         CloseResFile(theResFile);
  155.         ErrorBack(paramPtr, "Error: The file has no 'vers' resource.");
  156.         UnlockParams(paramPtr, kNumParams);
  157.         return;
  158.     }
  159.         
  160.     /* we have at least one vers resource.  Try to load vers resource ID 1 first.  If that
  161.         gives us a NIL or an error, try for vers resource ID 2.  If THAT yields NIL or an
  162.         error, give up and go home.  */
  163.     versHandle = (VersRecHndl) Get1Resource('vers',1);
  164.     theError = ResError();
  165.     if ( (versHandle == NIL) || (theError != noErr) ) {
  166.         versHandle = (VersRecHndl) Get1Resource('vers',2);
  167.         theError = ResError();
  168.         if ( (versHandle == NIL) || (theError != noErr) ) {
  169.             UseResFile(orgResFile);
  170.             SetResLoad(orgResLoad);
  171.             CloseResFile(theResFile);
  172.             ErrorBack(paramPtr, "Error: The 'vers' resource could not be opened.");
  173.             UnlockParams(paramPtr, kNumParams);
  174.             return;
  175.         }
  176.     }
  177.     
  178.     /* get the nation code out. */
  179.     nationCode = (**versHandle).countryCode;
  180.     NumToString((long) nationCode, nationStr);
  181.     p2cstr(nationStr);
  182.     
  183.             
  184.     /* clean up, then go home. */
  185.     SetResLoad(orgResLoad);
  186.     UseResFile(orgResFile);
  187.     CloseResFile(theResFile);
  188.     ErrorBack(paramPtr, nationStr);
  189.     UnlockParams(paramPtr, kNumParams);
  190.     return;
  191.  
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.     
  199. /* allocate and load up paramPtr->returnValue with a string 
  200.    -------------------------------------------------------- */
  201. void ErrorBack(XCmdPtr paramPtr, char *message)
  202. {
  203.     Handle  mesHnd;
  204.  
  205.     /*
  206.         Allocate space for an error message.
  207.         Copy the string into it.
  208.         Return the handle to HyperCard.
  209.     */
  210.     mesHnd = NewHandle((long)(strlen(message)+1));
  211.     if (mesHnd == nil) return;
  212.     strcpy((char *)*mesHnd,message);
  213.     paramPtr->returnValue = mesHnd;
  214. }
  215.  
  216.  
  217.  
  218. /*  move high and lock down all parameters  
  219.     ----------------------------------------------------------------------- */
  220. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  221. {
  222.     short i;
  223.     
  224.     for(i=0; i <= paramCount-1; i++)
  225.     {
  226.         MoveHHi(paramPtr->params[i]);
  227.         HLock(paramPtr->params[i]);
  228.     }
  229. }
  230.  
  231.  
  232.  
  233.  
  234. /* unlock all parameter handles in the XCmdBlock  
  235.    ---------------------------------------------  */
  236. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  237. {    short i;
  238.     
  239.     for(i=0; i <= paramCount-1; i++)
  240.         { HUnlock(paramPtr->params[i]);}
  241. }
  242.